local matSphere = Material("models/shadertest/shader3")

function EFFECT:Init(data)
	self.StartPos 	= data:GetOrigin()
	self.Speed	= data:GetMagnitude()
	self.Init 	= CurTime()	
	self.Time 	= self.Init
	self.End	= data:GetScale()
	self.Rad	= 0
	self.Rel	= 0
	self.Mul	= {}

	for i=1,3 do
		self.Mul[i] = {}
		self.Mul[i].Vec = Vector()
		self.Mul[i].Ang = Angle()
	end
	
	self.Entity:SetModel(Model("models/zup/shields/1024_shield.mdl"))
	self.Entity:SetPos(self.StartPos)
	self.Entity:SetRenderBounds(-1*Vector()*100000,Vector()*100000)
end

function EFFECT:Think()
	self.Time = CurTime()
	self.Rel = self.Time-self.Init
	self.Rad = self.Rel+math.Clamp(0.2*self.Rel^2-1, -1 ,2.2) -- This should account for the lag, but it will make it look better rather than just adding 1.5 to it.
	if self.Rel > self.End then return end
	local ftime = FrameTime()
	
	for i=1,3 do -- This is a bunch of acumulators and the like for making the transitions smooth
		self.Mul[i].Vec.x = math.sin(0.2*(self.Rad+self.Rad*math.cos(self.Rad))+i+1)
		self.Mul[i].Vec.y = math.cos(0.2*(self.Rad+self.Rad*math.cos(self.Rad))+i/2+0.5)		
		self.Mul[i].Vec.z = math.sin(0.2*(self.Rad+self.Rad*math.sin(self.Rad))+i+2)
		self.Mul[i].Ang.y = self.Mul[i].Ang.y+self.Mul[i].Vec.z*30*ftime
		self.Mul[i].Ang.p = self.Mul[i].Ang.p+self.Mul[i].Vec.y*30*ftime
		self.Mul[i].Ang.r = self.Mul[i].Ang.r+self.Mul[i].Vec.x*30*ftime
	end
	return true
end

function EFFECT:Render()
	local rad = self.Rad*self.Speed/1024
	
	SetMaterialOverride(matSphere)
	matSphere:SetMaterialFloat("$alpha", (1-self.Rel/self.End)*255)
	
	if StarGate.Visuals("cl_dakara_refract") then
		matSphere:SetMaterialFloat("$refractamount", 0.4)
	else
		matSphere:SetMaterialFloat("$refractamount", 0)
	end
	
	render.UpdateRefractTexture()
	
	self.Entity:SetModelScale(Vector()*(rad-0.1))
	self.Entity:DrawModel()
	
	for i=1,3 do 
		self.Entity:SetModelScale(Vector()*rad+self.Mul[i].Vec*rad/3)
		self.Entity:SetAngles(self.Mul[i].Ang)
		self.Entity:DrawModel()
	end
	SetMaterialOverride(nil)
end